package com.example.swipytabs;

import java.util.Locale;
import java.util.Random;

import com.example.swipytabs.EditNameDialog.EditNameDialogListener;
import com.example.swipytabs.R.color;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class TabNumberQuiz extends Fragment implements OnInitListener, EditNameDialogListener {
    public TabNumberQuiz() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

     
    TextView textView1 ,textViewTest, textViewResults;
    Button tab2ButtonChinese;
    Button buttonNum0, buttonNum1, buttonNum2,buttonNum3,buttonNum4,buttonNum5,buttonNum6,buttonNum7,buttonNum8,buttonNum9, buttonEnter, buttonNext;
    Button buttonPlay, buttonClear;
    EditText editTextNumberEntered;
    View view2;
    TextToSpeech talker;
    String textToSpeek = "Default";
    Integer intTextToSpeek = 0;
    Locale languageLocale = Locale.US;  //default locale
    Random randomNumber = new Random();
    Integer numRight = 0, numWrong = 0;
    RadioGroup radioGroupNumberRange;
    Integer numberRange = 20;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View v = inflater.inflate(R.layout.tabnumberquiz, null);
    	textView1 = (TextView)v.findViewById(R.id.textView1);

    	textViewTest = (TextView)v.findViewById(R.id.textViewTest);
    	textViewResults = (TextView)v.findViewById(R.id.textViewResults);
    	editTextNumberEntered = (EditText) v.findViewById(R.id.editText1);
    	
    	buttonPlay = (Button) v.findViewById(R.id.buttonPlay);
    	buttonPlay.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			textToSpeek = Integer.toString(intTextToSpeek); //replay current number
    			say(textToSpeek);
    		}
    	});	
    	
    	buttonClear = (Button) v.findViewById(R.id.buttonClear);
    	buttonClear.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			editTextNumberEntered.setText(""); //clear the entered number
    		}
    	});	
    	
    	buttonNum0 = (Button) v.findViewById(R.id.buttonNum0);
    	buttonNum0.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum0Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
 
    	buttonNum1 = (Button) v.findViewById(R.id.buttonNum1);
    	buttonNum1.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum1Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	buttonNum2 = (Button) v.findViewById(R.id.buttonNum2);
    	buttonNum2.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum2Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	   	    	
 
    	buttonNum3 = (Button) v.findViewById(R.id.buttonNum3);
    	buttonNum3.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum3Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	buttonNum4 = (Button) v.findViewById(R.id.buttonNum4);
    	buttonNum4.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum4Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	
    	buttonNum5 = (Button) v.findViewById(R.id.buttonNum5);
    	buttonNum5.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum5Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	  
    	
    	buttonNum6 = (Button) v.findViewById(R.id.buttonNum6);
    	buttonNum6.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum6Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	
    	buttonNum7 = (Button) v.findViewById(R.id.buttonNum7);
    	buttonNum7.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum7Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	    
    	
    	buttonNum8 = (Button) v.findViewById(R.id.buttonNum8);
    	buttonNum8.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum8Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	   	
    	
    	buttonNum9 = (Button) v.findViewById(R.id.buttonNum9);
    	buttonNum9.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNum9Click(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	buttonEnter = (Button) v.findViewById(R.id.buttonEnter);
    	buttonEnter.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonEnterClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
  	
    	tab2ButtonChinese = (Button) v.findViewById(R.id.buttonTab2CheckChinese);
    	tab2ButtonChinese.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onTab2ButtonChineseClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});
  	
    	
    	buttonNext = (Button) v.findViewById(R.id.buttonNext);
    	buttonNext.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onbuttonNextClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});    	
    	
    	
    	radioGroupNumberRange = (RadioGroup) v.findViewById(R.id.radioGroup1);
    	radioGroupNumberRange.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onRadioGroupNumberRangeClick();
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});	
    	
    	
        return v;
    }
    
    protected void onRadioGroupNumberRangeClick() {
		// TODO Auto-generated method stub
    	
		//Toast.makeText(getActivity(), "Radio Button Pressed", Toast.LENGTH_LONG).show();

		switch (radioGroupNumberRange.getCheckedRadioButtonId()){
			case R.id.radio0:
				numberRange = 19;
				break;
			case R.id.radio1:
				numberRange = 999;
				break;
			case R.id.radio2:
				numberRange = 9999;
				break;
		}
	}

	protected void onbuttonNextClick(View v) {
		// TODO Auto-generated method stub
		onRadioGroupNumberRangeClick();
    	editTextNumberEntered.setBackgroundColor(Color.GRAY);
		String temp = "";
		editTextNumberEntered.setText(temp);
		textViewTest.setText("");
		onTab2ButtonChineseClick(v);
	}

	protected void onbuttonNum9Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"9";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum8Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"8";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum7Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"7";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum6Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"6";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum5Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"5";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum4Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"4";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonEnterClick(View v) {
		// TODO Auto-generated method stub
		int answer = Integer.parseInt(editTextNumberEntered.getText().toString());
		if (checkAnswer(intTextToSpeek, answer )){
			editTextNumberEntered.setBackgroundColor(Color.GREEN);
		}
		else{
			editTextNumberEntered.setBackgroundColor(Color.RED);
			textViewTest.setText(textToSpeek);
		}  

	}

	protected void onbuttonNum3Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"3";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum2Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"2";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum1Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"1";
		editTextNumberEntered.setText(temp);
	}

	protected void onbuttonNum0Click(View v) {
		// TODO Auto-generated method stub
		String temp = editTextNumberEntered.getText()+"0";
		editTextNumberEntered.setText(temp);
	}

	public void say(String text2say){
    	talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
    }
    
    public void onTab2ButtonChineseClick(View v){
    	//member to handle the button click
    	Activity activity = getActivity();

    	
    	talker = new TextToSpeech(getActivity(), this);

    	//textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
		//int results = talker.isLanguageAvailable(Locale.US);
		//textView4.setText(checkLanguageAvailability(results));
    	
    	//say("三十九");
    	languageLocale = Locale.CHINESE;
    	//textToSpeek = "I can speek Chinese";
    	//textToSpeek = "我说一点点中文";
    	//textToSpeek = "1234";
    	//intTextToSpeek = randomNumber.nextInt(200);
    	intTextToSpeek = randomNumber.nextInt(numberRange);  //numberRange is set by radio buttons
    	textToSpeek = Integer.toString(intTextToSpeek);
    	//textView4.setText(textToSpeek);
    	//Toast.makeText(activity, "Tab2 Chinese Button Clicked", Toast.LENGTH_SHORT ).show(); 
    	//showEditDialog(textToSpeek);
    	
    }

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
				
 /*       //check for successful instantiation
		if (status == TextToSpeech.SUCCESS) {
			if(talker.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
				talker.setLanguage(Locale.US);
				talker.speak(textToSpeek, TextToSpeech.QUEUE_FLUSH, null);
				//int results = talker.isLanguageAvailable(Locale.US);
				//textView4.setText(checkLanguageAvailability(results));
			}
		else if (status == TextToSpeech.ERROR) {
			Toast.makeText(getActivity(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
			} */

		//check for successful instantiation
		if (status == TextToSpeech.SUCCESS) {
			int results = talker.isLanguageAvailable(languageLocale);
			if(results == TextToSpeech.LANG_AVAILABLE)
				talker.setLanguage(languageLocale);
				//textView1.setText(checkLanguageAvailability(results));
				talker.speak(textToSpeek, TextToSpeech.QUEUE_FLUSH, null);
				
			}
		else if (status == TextToSpeech.ERROR) {
			Toast.makeText(getActivity(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
			}		
	}

	@Override
	public void onDestroy() {
		if (talker != null) {
			talker.stop();
			talker.shutdown();
		}
		super.onDestroy();
	}
	
	public String checkLanguageAvailability(int results){
		String text = "text";
		
		switch (results){
			case TextToSpeech.LANG_NOT_SUPPORTED:
				text = Integer.toString(results)+": Language Not Supported";
				break;
			case TextToSpeech.LANG_AVAILABLE:
				text = "Language Available";
				break;
			case TextToSpeech.LANG_COUNTRY_AVAILABLE:
				text = "Language Country Available";
				break;				
			default:
				text = Integer.toString(results);
 	}
		
		return text;
		
	}

    private void showEditDialog(String titlePrompt) {
        FragmentManager fm = getFragmentManager();
        EditNameDialog editNameDialog = new EditNameDialog(titlePrompt);
        //editNameDialog.show(fm, "dlg_edit_name");
        editNameDialog.show(fm, "who is me");
    }

    @Override
    public void onFinishEditDialog(String inputText) {
        //Toast.makeText(getActivity(), "Hi, " + inputText, Toast.LENGTH_SHORT).show();
    	//textView2.setText(inputText);
    }  	
    
	protected boolean checkAnswer(int test, int answer) {
		// method to check if answer is correct
		boolean results;
		if (test == answer){
			editTextNumberEntered.setBackgroundColor(color.glowing_font_color);
			numRight++;
			results = true;
		}
		else{
			editTextNumberEntered.setBackgroundColor(Color.RED);
			numWrong++;
			results = false;
		}

		textViewResults.setText(Integer.toString(numRight)+" Correct Out of "+Integer.toString(numRight+numWrong));
		return results;
	}
	
}
